//
//  ADStatusViewModel.swift
//  myBlog
//
//  Created by mac2014 on 16/3/27.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit
//微博数据的逻辑代码
class ADStatusViewModel: NSObject {
    //创建类的单例，方便调用
    static let sharedInstance: ADStatusViewModel = ADStatusViewModel()
    
    //加载微博数据
    func loadStatuses(loadStatusesCallBack:(statuses:[ADStatus]?, error: NSError?) -> ()) {
        //数据链接
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parameters = ["access_token":ADUserAcountViewModel.sharedInstance.userAccount!.access_token!]
        //print("-----\(ADUserAcountViewModel.sharedInstance.userAccount!.access_token!)")
        
        //网络返回数据链接
        //https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.00UjcbeCXZY9ID6b1d625a62BZRyIE
        
        ADNetWorkingTool.sharedInstance.GET(urlString, parameters: parameters, progress: nil) { (result, error) -> () in
            if error != nil {
                return
            }
            //创建一个数组来存放微博状态数据
            var statuses = [ADStatus]()
            //print("result:\(result)")
            //将请求得到的数据存放在一个由字典组成的数组中
            let statusesArr = result?["statuses"] as! [[String: AnyObject]]
            
            //遍历
            for dict in statusesArr {
                //每条状态的数据
                let status = ADStatus(dict: dict)
                
                statuses.append(status)
            }
            //字典转模型成功：返回模型数据
            loadStatusesCallBack(statuses: statuses, error: nil)
        }
    }
}
